package cs341.group2.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import cs341.group2.mycontacts.Database.Category;
import cs341.group2.mycontacts.Database.Contact;
import cs341.group2.mycontacts.Database.MyContactsDB;

public class ViewCategory extends Activity {
	private MyContactsDB myContactsDb;
	private int categoryId;
	private String currentContactId;
	private AlertDialog bri;
	ListView list;
	
	@SuppressLint("NewApi")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_category);
        myContactsDb = new MyContactsDB(this);
        
        Button b=(Button)findViewById(R.id.button_groups);
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ViewCategory.this, cs341.group2.mycontacts.MainActivity.class);
				startActivity(i);
			}
		});
        
        Button b2=(Button)findViewById(R.id.button_contact);
        b2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i2 = new Intent(ViewCategory.this, cs341.group2.mycontacts.ContactPage.class);
				i2.putExtra("state", "add");
				startActivity(i2);
			}
		});
        
               
        
        //pull in passed in parameters
        Bundle extras = getIntent().getExtras();
        String name = extras.getString("Name");
        categoryId = extras.getInt("Id");
        final EditText text = (EditText)findViewById(R.id.categoryName);
        text.setText(name);
        text.setId(categoryId);
        
      //Grab delete category button and bind call to it
        Button c =(Button)findViewById(R.id.DeleteCategoryButton);
        c.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub

				AlertDialog.Builder build  = new AlertDialog.Builder(ViewCategory.this);
				build.setTitle("Warning");
				build.setMessage("Do you want to delete this category");
				build.setPositiveButton("yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						new DeleteCategory().execute(categoryId);
					}
				});
				build.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						bri.cancel();
					}
				});
				bri =build.create();
				bri.show();

			}
		});
        
        
      //on click function for update category name
        final Button b3=(Button)findViewById(R.id.UpdateCategoryNameButton);
        b3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Category cat = new Category();
				cat.Name = text.getText().toString();
				cat.Id = text.getId();
				new UpdateCategory().execute(cat);
			}
		});
        
    }
	
	public void populateContactsList(Contact[] contacts){
		ListView list = (ListView)findViewById(R.id.ContactsListView);
		list.setClickable(true);
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		for(int i = 0; i < contacts.length; i++){
	        map = new HashMap<String, String>();
	        map.put("contactName", contacts[i].Name);
	        map.put("contactId", String.valueOf(contacts[i].Id));
	        map.put("contactRemove", "Remove");
	        listItem.add(map);
		}
		ButtonAdapter adapter = new ButtonAdapter (this.getBaseContext(), listItem);
		list.setAdapter(adapter);
	}
	
	public void BackToHomePage(){
		Intent i = new Intent(ViewCategory.this, cs341.group2.mycontacts.MainActivity.class);
		startActivity(i);
	}
	
	@Override
	public void onResume() {
		super.onResume(); // Always call the superclass method first

		new GetAllContactsForCategory().execute((Object[]) null);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private class GetAllContactsForCategory extends AsyncTask<Object, Object, Contact[]> {
		public MyContactsDB myContactsDb = new MyContactsDB(ViewCategory.this);

		@Override
		protected Contact[] doInBackground(Object... params) {
			myContactsDb.open();
			return myContactsDb.getAllContactsForCategory(categoryId);
			//return myContactsDb.getAllContacts();
		}

		@Override
		protected void onPostExecute(Contact[] contacts) {
			myContactsDb.close();
			populateContactsList(contacts);
		}
	}
	
	private class DeleteCategory extends AsyncTask<Integer, Object, Object> {
		public MyContactsDB myContactsDb = new MyContactsDB(ViewCategory.this);

		protected Object doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			myContactsDb.open();
			myContactsDb.deleteCategory(params[0]);
			return (Object[]) null;
		}
		
		protected void onPostExecute(Object o) {
			myContactsDb.close();
			BackToHomePage();
		}
	}
	
	private class UpdateCategory extends AsyncTask<Category, Object, Object> {
		public MyContactsDB myContactsDb = new MyContactsDB(ViewCategory.this);

		protected Object doInBackground(Category... params) {
			// TODO Auto-generated method stub
			myContactsDb.open();
			myContactsDb.UpdateCategoryName(params[0].Name, params[0].Id);
			return (Object[]) null;
		}
		
		protected void onPostExecute(Object o) {
			myContactsDb.close();
		}
	}
			
}
